﻿/**
  ******************************************************************************
  * @file    Libraries/Device/JS32T031/JS32T031_LL_Driver/inc/js32t031_ll_uart.h
  * @author  JUSHENG Application Team
  * @version V1.0.0
  * @date    02-19-2022
  * @brief   This file contains all the UART LL firmware functions.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2022 JUSHENG</center></h2>
  *
  *
  *
  ******************************************************************************
  */ 
  
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __JS32T031_LL_UART_H
#define __JS32T031_LL_UART_H

#ifdef __cplusplus
 extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "js32t031.h"
     
/** @addtogroup JS32T031_StdPeriph_Driver JS32T031 Driver
  * @{
  */
     
/** @addtogroup uart_interface_gr UART Driver
  * @ingroup  JS32T031_StdPeriph_Driver
  * @{
  */ 
     
/** @addtogroup UART_LL_Driver UART LL Driver
  * @ingroup  uart_interface_gr
  * @brief Mainly the driver part of the UART module, which includes \b UART \b Register 
  * \b Constants, \b UART \b Exported \b Constants, \b UART \b Exported \b Struct, \b UART
  * \b Data \b transfers \b functions, \b UART \b Initialization \b and \b UART \b Configuration 
  * \b And \b Interrupt \b Handle \b function.
  * @{
  */
     
/* Exported types ------------------------------------------------------------*/

/* Exported constants --------------------------------------------------------*/
     
/** @defgroup UART_LL_Register_Constants UART LL Register Constants
  * @ingroup  UART_LL_Driver
  * @brief    UART LL register constant table definition
  *
@verbatim   
  ===============================================================================
                                Register Constants
  ===============================================================================  
  
    Register Constants mainly encapsulates each bit in each group in the UART 
    register. In the process of configuration, the macro definition can be directly 
    called to configure the UART register, mainly for convenience. Understand the 
    configuration of the UART.
    
@endverbatim
  *
  * @{
  */

/***** UARTCON(Uart cConfig Register) *****/
/*! Close the UART RX function  
 * 0 : disable  
 * 1 : enable  
 */
#define LL_UARTX_CON_RX_CLOSE_EN                            (1UL << 31)

/*! Time out time configure.unit is bit rate time.  
 * Time_out_time = (TO_BIT_LEN+1) * bit_rate_time  
 */
#define LL_UARTX_CON_UART_TO_BIT_LEN_SET(n)                 ((u32)(n & 0x3FFF)<<16)
 
 /*! Transmission complete interrupt enable  
 * 0 : disable interrupt  
 * 1 : An UART interrupt is generated whenever TC=1 in the UART_STA register  
 */
#define LL_UARTX_CON_UART_TC_IE                             (1UL << 15)

/*! Uart timer pwm en config(uart tx and timer pwm work together, uart1 output timer1's pwm)  
 * 0 : disable  
 * 1 : An UART interrupt is generated whenever TC=1 in the UART_STA register  
 */
#define LL_UARTX_CON_UART_TMR_PWM_EN                        (1UL << 14)

/*! Uart timeout interrupt en  
 * 0 : disable  
 * 1 : enable  
 */
#define LL_UARTX_CON_UART_TO_IE                             (1UL << 13)

/*! Uart rx timeout en config(check no received data again after (TO_BIT_LEN+1) * bit_rate_time times)  
 * 0 : disable  
 * 1 : enable  
 */
#define LL_UARTX_CON_UART_TO_EN                             (1UL << 12)

/*! Uart frame err interrupt en config  
 * 0 : disable  
 * 1 : enable  
 */
#define LL_UARTX_CON_UART_FERR_IE                           (1UL << 11)

/*! Uart send buffer empty interrupt en config  
 * 0 : disable  
 * 1 : enable  
 */
#define LL_UARTX_CON_UART_TXBUF_EMPTY_IE                    (1UL << 10)

/*! Uart receive buffer not empty interrupt en config  
 * 0 : disable  
 * 1 : enable  
 */
#define LL_UARTX_CON_UART_RXBUF_NOT_EMPTY_IE                (1UL << 9)

/*! Uart tx inv config  
 * 0 : no tx inv  
 * 1 : tx inv  
 */
#define LL_UARTX_CON_UART_TX_INV_EN                         (1UL << 8)

/*! Uart rx inv config  
 * 0 : no rx inv  
 * 1 : rx inv  
 */
#define LL_UARTX_CON_UART_RX_INV_EN                         (1UL << 7)

/*! Uart odd parity en config  
 * 0 : even parity  
 * 1 : odd parity  
 */
#define LL_UARTX_CON_UART_ODD_EN                            (1UL << 6)

/*! Uart parity en config(parity and bit9 choose either-or)  
 * 0 : disable  
 * 1 : enable  
 */
#define LL_UARTX_CON_UART_PARITY_EN                         (1UL << 5)

/*! Uart bit9 en config(parity and bit9 choose either-or)  
 * 0 : uart rx tx 8bit  
 * 1 : uart rx tx 9bit  
 */
#define LL_UARTX_CON_UART_9BIT_EN                           (1UL << 4)

/*! Uart stop bit config  
 * 0 : 1 stop bit  
 * 1 : 2 stop bit  
 */
#define LL_UARTX_CON_UART_STOP_BIT                          (1UL << 3)


/*! Uart work mode config(rw).  
 * 0 : Full duplex mode  
 * 1 : A data line, software triggers to send IO is output, no data sent IO is input  
 */
 #define LL_UARTX_CON_UART_WORK_MODE                        (1UL << 1)

/*! Uart enable config  
 * 1 : enable  
 * 0 : disable   
 */
#define LL_UARTX_CON_UART_EN                                (1UL << 0)


/***** STA(Uart Work Status Register) *****/


/*! Transmission complete.  
 * This bit is set by hardware if the transmission of a frame is complete and if TX_BUF_EMPTY is set.  
 * An interrupt is generated if TCIE=1 in the UARTCON register.  
 * It is cleared by a software sequence (a write to the UART_DR register). The TC bit can also be cleared by writing a '1' to it.  
 * 1 : Transmission is not complete  
 * 0 : Transmission is complete  
 */
#define LL_UARTX_STA_TC_PENDING                             (1UL << 15)

/*! Rx timeout flag, this bit is set received data again after 5 *£¨1 start_bit + 8 data bit + 2 stop bit£©times, write 1 clear 0  
 * 1 : timeout  
 * 0 : no timeout  
 */
#define LL_UARTX_STA_RX_TIMEOUT_PENDING                     (1UL << 11)

/*! Odd even parity err pending  
 * corresponding to Rx buf 4 data. Read this registor before read  UARTDATA. PERR[0] corresponding to first read UARTDATA.  
 * 1 : frame err  
 * 0 : frame not err  
 */
#define LL_UARTX_STA_RX_PERR_PENDING                        (0x0F << 7)

/*! Rx buf data number  
 * 000: 0 data  
 * 001: 1 data  
 * 010: 2 data  
 * 011: 3 data  
 * 100: 4 data  
 * other: not allow  
 */
#define LL_UARTX_STA_RX_CNT                                 (7UL << 4)

/*! Frame err flag  
 * This bit is set if rx low level during stopbit  
 * 1 : frame err  
 * 0 : frame not err  
 */
#define LL_UARTX_STA_RX_FERR_PENDING                        (1UL << 3)

/*! Rx buf overflow flag  
 * Rx buf holds up 4 8(9)bits data, or else this flag is 1, write 1 clear 0.  
 * 1 : Receive > 4 bytes data , rx buf save headmost 4 bytes data, other data discard.  
 * 0 : Receive 0~4 bytes data  
 */
#define LL_UARTX_STA_RX_BUF_OV_PENDING                      (1UL << 2)

/*! Rx buf not empty flag, 0 : rx buf has no data , 1 : rx buf has data, write 1 clear 0.  
 */
#define LL_UARTX_STA_RX_BUF_NOT_EMPTY_PENDING               (1UL << 1)

/*! Tx buffer empty flag, 
 * Send BUF empty when the position is 1.  
 * UART is sending data, software writes register UARTDATA, this position 1  
 * 1 : buffer empty  
 * 0 : Tx buffer full or UART send not enabled  
 */
#define LL_UARTX_STA_TX_BUF_EMPTY_PENDING                   (1UL << 0)

/*! Sta perr offset
 */
#define LL_UARTX_STA_RX_PERR_OFFSET                         (0x07)

/*! Sta rx cnt offset
 */
#define LL_UARTX_STA_RX_CNT_OFFSET                          (0x04)

/*! Uart baud
 */ 
#define LL_UARTx_BAUDRATE_300                               (300)
#define LL_UARTx_BAUDRATE_600                               (600)
#define LL_UARTx_BAUDRATE_2400                              (2400)
#define LL_UARTx_BAUDRATE_1200                              (1200)
#define LL_UARTx_BAUDRATE_4800                              (4800)
#define LL_UARTx_BAUDRATE_9600                              (9600)
#define LL_UARTx_BAUDRATE_19200                             (19200)
#define LL_UARTx_BAUDRATE_38400                             (38400)
#define LL_UARTx_BAUDRATE_115200                            (115200)
#define LL_UARTx_BAUDRATE_256K                              (256000)
#define LL_UARTx_BAUDRATE_300K                              (300000)
#define LL_UARTx_BAUDRATE_460800                            (460800)
#define LL_UARTx_BAUDRATE_500K                              (500000)
#define LL_UARTx_BAUDRATE_600K                              (600000)
#define LL_UARTx_BAUDRATE_750K                              (750000)
#define LL_UARTx_BAUDRATE_921600                            (921600)
#define LL_UARTx_BAUDRATE_1M                                (1000000)
#define LL_UARTx_BAUDRATE                                   LL_UARTx_BAUDRATE_115200

/**
  * @}
  */

/** @defgroup UART_LL_Exported_Constants UART LL Exported Constants
  * @ingroup  UART_LL_Driver
  * @brief    UART LL external constant definition
  *
@verbatim   
  ===============================================================================
                                Exported Constants
  ===============================================================================  
  
    Exported Constants mainly restricts the partial configuration of the abstraction 
    layer by using the form of enumeration to facilitate the use and understanding of 
    the module configuration. For the specific enumeration meaning, please refer to 
    the annotation of each module.

@endverbatim
  *
  * @{
  */
/***** DRIVER API *****/


/***** LL API *****/
/**
  * @brief Enumeration constant for UART data length(9bit no parity)
  */
typedef enum {
    /*! Uart word length select 8 bit
     */
    LL_UART_WORD_LENGTH_8B      = 0,
    /*! Uart word length select 9 bit
     */
    LL_UART_WORD_LENGTH_9B,
} TYPE_ENUM_LL_UART_BIT_WIDTH_SEL;

/**
  * @brief Enumeration constant for UART parity select
  */
typedef enum {
    /*! Uart no parity check
     */
    LL_UART_PARITY_NO           = 0,
    /*! Uart odd parity
     */
    LL_UART_PARITY_ODD,
    /*! Uart enven parity
     */    
    LL_UART_PARITY_EVEN,
    /*! Reserver
     */
    LL_UART_RESVERS,
} TYPE_ENUM_LL_UART_PARITY_SEL;

/**
  * @brief Enumeration constant for UART stop bit select
  */
typedef enum {
    /*! Uart stop bit select 1 bit
     */
    LL_UART_STOP_1B             = 0,
    /*! Uart stop bit select 2 bit
     */
    LL_UART_STOP_2B,
} TYPE_ENUM_LL_UART_STOP_BIT_SEL;

/**
  * @brief Enumeration constant for UART rx ie
  */
typedef enum {
    /*! Uart rx ie disable
     */    
    LL_UART_RX_IE_DISABLE       = 0,
    /*! Uart rx ie enable
     */   
    LL_UART_RX_IE_ENABLE,
} TYPE_ENUM_LL_UART_RX_IE;

/**
  * @brief Enumeration constant for UART tx ie
  */
typedef enum {
    /*! Uart tx ie disable
     */ 
    LL_UART_TX_IE_DISABLE       = 0,
    /*! Uart tx ie enable
     */
    LL_UART_TX_IE_ENABLE
} TYPE_ENUM_LL_UART_TX_IE;

/**
  * @brief Enumeration constant for UART rx invert
  */
typedef enum {
    /*! Uart rx invert disable
     */
    LL_UART_RX_INV_DISABLE      = 0,
    /*! Uart rx invert enable
     */
    LL_UART_RX_INV_ENABLE,
} TYPE_ENUM_LL_UART_RX_INV;

/**
  * @brief Enumeration constant for UART tx invert
  */
typedef enum {
    /*! Uart tx invert disable
     */
    LL_UART_TX_INV_DISABLE      = 0,
    /*! Uart tx invert enable
     */
    LL_UART_TX_INV_ENABLE,
} TYPE_ENUM_LL_UART_TX_INV;

/**
  * @brief Enumeration constant for UART en
  */
typedef enum {
    /*! Uart disable
     */
    LL_UART_DISABLE             = 0,
    /*! Uart enable
     */
    LL_UART_ENABLE,
} TYPE_ENUM_LL_UART_EN;

/**
  * @brief Enumeration constant for UART bit9 en
  */
typedef enum {
    /*! Uart bit9 disable
     */
    LL_UART_9BIT_DISABLE        = 0,
    /*! Uart bit9 enable
     */
    LL_UART_9BIT_ENABLE,
} TYPE_ENUM_LL_UART_BIT9_EN;

/**
  * @brief Enumeration constant for UART odd or even parity en
  */
typedef enum {
    /*! Uart even parity enable
     */
    LL_UART_PARITY_EVEN_ENABLE  = 0,
    /*! Uart odd parity enable
     */
    LL_UART_PARITY_ODD_ENABLE,
} TYPE_ENUM_LL_UART_ODD_EN;

/**
  * @brief Enumeration constant for UART parity en
  */
typedef enum {
    /*! Uart parity disable
     */
    LL_UART_PARITY_DISABLE      = 0,
    /*! Uart parity enable
     */
    LL_UART_PARITY_ENABLE,
} TYPE_ENUM_LL_UART_PARITY_EN;

/**
  * @brief Enumeration constant for UART work mode
  */
typedef enum {
    /*! Uart work mode duplex
     */
    LL_UART_WORK_MODE_FULL_DUPLEX = 0,
    /*! Uart work mode single auto
     */
    LL_UART_WORK_MODE_SINGLE_AUTO,
} TYPE_ENUM_LL_UART_WORK_MODE;


/***** LL API AND DRIVER API *****/


/**
  * @}
  */

/** @defgroup UART_LL_Exported_Struct UART LL Exported Struct
  * @ingroup  UART_LL_Driver
  * @brief    UART LL external configuration structure definition
  *
@verbatim   
  ===============================================================================
                                Exported Struct
  ===============================================================================  

    Exported Struct mainly extracts the UART registers from the API, and abstracts 
    the structure. As long as it implements the low coupling between the registers 
    and the registers, the user only needs to configure the structure of the abstraction 
    layer and call hal_uart_init. Function, you can configure the UART module without 
    involving the configuration of the collective register.

@endverbatim
  *
  * @{
  */

/**
  * @brief UART low layer configuration structure
  */
typedef struct __ll_uart_init {
    /*! Time out time configure.unit is bit rate time.
    */
    u16                                 timeout_len;
    /*! UART baud
     */
    u32                                 baudrate;
    /*! UART data length selection
     */
    TYPE_ENUM_LL_UART_BIT_WIDTH_SEL     bit_width_sel;
    /*! UART parity selection
     */
    TYPE_ENUM_LL_UART_PARITY_SEL        parity;
    /*! UART stop bit selection
     */
    TYPE_ENUM_LL_UART_STOP_BIT_SEL      stop_bit_sel;
    /*! UART work mode
     */
    TYPE_ENUM_LL_UART_WORK_MODE         work_mode;
} TYPE_LL_UART_INIT;

/**
  * @brief UART low layer interrupt configuration structure
  */
typedef struct __ll_uart_ie_cfg {
    /*! Enable send buffer empty interrupt
     */   
    FunctionalState                     uart_txbuf_empty_ie;
    /*! Enable receive buffer not empty interrupt
     */
    FunctionalState                     uart_rxbuf_not_empty_ie;
} TYPE_LL_UART_IE_CFG;

/**
  * @}
  */

/** @defgroup UART_LL_Interrupt UART LL Interrupt Handle function
  * @ingroup  UART_LL_Driver
  * @brief   UART LL Interrupt Handle function
  *
@verbatim   
  ===============================================================================
                        Interrupt Handle function
  ===============================================================================  

    This subsection provides a set of functions allowing to manage the UART  
    Interrupt Handle function.

    how to use?

@endverbatim
  *
  * @{
  */


/**
  * @}
  */
  
/** @defgroup UART_LL_Inti_Cfg UART LL Initialization And Configuration
  * @ingroup  UART_LL_Driver
  * @brief    UART LL Initialization And Configuration
  *
@verbatim   
  ===============================================================================
                        Initialization And Configuration
  ===============================================================================  

    This subsection provides a set of functions allowing to manage the UART data 
    Initialization and Configuration.
    
    how to use?

@endverbatim
  *
  * @{
  */

/**
  * @brief  Link layer UART initialization function
  * @param  p_uart: The structure pointer of the UART group (UART0) is selected.
  * @param  p_init : UART initialization struct
  * @retval None.
  */
void ll_uart_init(UART_TypeDef *p_uart, TYPE_LL_UART_INIT *p_init);

/**
  * @brief  Link layer UART interrupt initialization function
  * @param  p_uart: The structure pointer of the UART group (UART0) is selected.
  * @param  p_cfg  : UART Configuration struct
  * @retval None.
  */
void ll_uart_ie_config(UART_TypeDef *p_uart, TYPE_LL_UART_IE_CFG *p_cfg);

/**
  * @}
  */
  
/** @defgroup UART_LL_Data_Transfers UART LL Data transfers functions
  * @ingroup  UART_LL_Driver
  * @brief    UART LL Data transfers functions 
  *
@verbatim   
  ===============================================================================
                            Data transfers functions
  ===============================================================================  

    This subsection provides a set of functions allowing to manage the UART data 
    transfers and receive.
  
@endverbatim
  *
  * @{
  */

/**
  * @brief  Link layer UART tx complete interrupt enable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_tx_complete_interrupt_enable(UART_TypeDef *p_uart);
/**
  * @brief  Link layer UART tx complete interrupt disable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_tx_complete_interrupt_disable(UART_TypeDef *p_uart);
/**
  * @brief  Link layer UART tx complete interrupt get
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval result
  */
#define LL_UART_TX_COMPLETE_INTERRUPT_GET(p_uart)           ((p_uart)->CON & LL_UARTX_CON_UART_TC_IE)

/**
  * @brief  Link layer UART timeout interrupt enable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_rx_timeout_interrupt_enable(UART_TypeDef *p_uart);
/**
  * @brief  Link layer UART timeout interrupt disable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_rx_timeout_interrupt_disable(UART_TypeDef *p_uart);
/**
  * @brief  Link layer UART timeout interrupt get
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval result
  */
#define LL_UART_RX_TIMEOUT_INTERRUPT_GET(p_uart)            ((p_uart)->CON & LL_UARTX_CON_UART_TO_IE)

/**
  * @brief  Link layer UART frame err interrupt enable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_ferr_interrupt_enable(UART_TypeDef *p_uart);
/**
  * @brief  Link layer UART frame err interrupt disable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_ferr_interrupt_disable(UART_TypeDef *p_uart);
/**
  * @brief  Link layer UART frame err interrupt get
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval result
  */
#define LL_UART_FERR_INTERRUPT_GET(p_uart)                  ((p_uart)->CON & LL_UARTX_CON_UART_FERR_IE)

/**
  * @brief  Link layer UART send buffer empty interrupt enable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_tx_buf_empty_interrupt_enable(UART_TypeDef *p_uart);
/**
  * @brief  Link layer UART send buffer empty interrupt disable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_tx_buf_empty_interrupt_disable(UART_TypeDef *p_uart);
/**
  * @brief  Link layer UART send buffer empty interrupt get
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval result
  */
#define LL_UART_TX_BUF_EMPTY_INTERRUPT_GET(p_uart)          ((p_uart)->CON & LL_UARTX_CON_UART_TXBUF_EMPTY_IE)

/**
  * @brief  Link layer UART receive buffer not empty interrupt enable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_rx_buf_not_empty_interrupt_enable(UART_TypeDef *p_uart);
/**
  * @brief  Link layer UART receive buffer not empty interrupt disable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_rx_buf_not_empty_interrupt_disable(UART_TypeDef *p_uart);
/**
  * @brief  Link layer UART rx finish interrupt get
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval result
  */
#define LL_UART_RX_RXBUF_NOT_EMPTY_INTERRUPT_GET(p_uart)    ((p_uart)->CON & LL_UARTX_CON_UART_RXBUF_NOT_EMPTY_IE)

/**
  * @brief  Link layer UART tmr pwm output enable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_tmr_pwm_enable(UART_TypeDef *p_uart);
/**
  * @brief  Link layer UART tmr pwm output disable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_tmr_pwm_disable(UART_TypeDef *p_uart);
/**
  * @brief  Link layer UART tmr pwm output enable get
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval result
  */
#define LL_UART_TMR_PWM_EN_GET(p_uart)                      ((p_uart)->CON & LL_UARTX_CON_UART_TMR_PWM_EN)

/**
  * @brief  Link layer UART rx close enable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_rx_close_enable(UART_TypeDef *p_uart);
/**
  * @brief  Link layer UART rx close disable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_rx_close_disable(UART_TypeDef *p_uart);

/**
  * @brief  Link layer UART rx timeout enable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_rx_timeout_enable(UART_TypeDef *p_uart);
/**
  * @brief  Link layer UART rx timeout disable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_rx_timeout_disable(UART_TypeDef *p_uart);
/**
  * @brief  Link layer UART rx timeout enable get
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval result
  */
#define LL_UART_RX_TIMEOUT_EN_GET(p_uart)                   ((p_uart)->CON & LL_UARTX_CON_UART_TO_EN)

/**
  * @brief  Link layer UART tx inv enable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_tx_inv_enable(UART_TypeDef *p_uart);
/**
  * @brief  Link layer UART tx inv disable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_tx_inv_disable(UART_TypeDef *p_uart);
/**
  * @brief  Link layer UART tx inv enable get
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval result
  */
#define LL_UART_TX_INV_EN_GET(p_uart)                       ((p_uart)->CON & LL_UARTX_CON_UART_TX_INV_EN)

/**
  * @brief  Link layer UART rx inv enable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_rx_inv_enable(UART_TypeDef *p_uart);
/**
  * @brief  Link layer UART rx inv disable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_rx_inv_disable(UART_TypeDef *p_uart);
/**
  * @brief  Link layer UART rx inv enable get
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval result
  */
#define LL_UART_RX_INV_EN_GET(p_uart)                       ((p_uart)->CON & LL_UARTX_CON_UART_RX_INV_EN)

/**
  * @brief  Link layer UART odd enable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_odd_enable(UART_TypeDef *p_uart);
/**
  * @brief  Link layer UART odd disable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_odd_disable(UART_TypeDef *p_uart);
/**
  * @brief  Link layer UART odd enable get
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval result
  */
#define LL_UART_ODD_EN_GET(p_uart)                          ((p_uart)->CON & LL_UARTX_CON_UART_ODD_EN)

/**
  * @brief  Link layer UART parity enable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_parity_enable(UART_TypeDef *p_uart);
/**
  * @brief  Link layer UART parity disable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_parity_disable(UART_TypeDef *p_uart);
/**
  * @brief  Link layer UART parity enable get
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval result
  */
#define LL_UART_PARITY_EN_GET(p_uart)                       ((p_uart)->CON & LL_UARTX_CON_UART_PARITY_EN)

/**
  * @brief  Link layer UART 9bit enable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_9bit_enable(UART_TypeDef *p_uart);
/**
  * @brief  Link layer UART 9bit disable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_9bit_disable(UART_TypeDef *p_uart);
/**
  * @brief  Link layer UART 9bit enable get
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval result
  */
#define LL_UART_9BIT_EN_GET(p_uart)                         ((p_uart)->CON & LL_UARTX_CON_UART_9BIT_EN)

/**
  * @brief  Link layer UART enable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_enable(UART_TypeDef *p_uart);
/**
  * @brief  Link layer UART disable
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
void ll_uart_disable(UART_TypeDef *p_uart);
/**
  * @brief  Link layer UART enable get
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval result
  */
#define LL_UART_EN_GET(p_uart)                              ((p_uart)->CON & LL_UARTX_CON_UART_EN)

/**
  * @brief  Link layer UART rx perr pending get
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
#define LL_UART_RX_PERR_PENDING_GET(p_uart)                         (((p_uart->STA) & LL_UARTX_STA_RX_PERR_PENDING) >> LL_UARTX_STA_RX_PERR_OFFSET)

/**
  * @brief  Link layer UART rx buf data num get
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval None
  */
#define LL_UART_RX_CNT_GET(p_uart)                          (((p_uart->STA) & LL_UARTX_STA_RX_CNT) >> LL_UARTX_STA_RX_CNT_OFFSET)

/**
  * @brief  Link layer UART Transmission complete pending get
  * @retval result
  */
#define LL_UART_TX_COMPLETE_PENDING_GET(p_uart)             ((p_uart)->STA & LL_UARTX_STA_TC_PENDING)
/**
  * @brief  Link layer UART Transmission complete pending clr
  * @retval None
  */
#define LL_UART_TX_COMPLETE_PENDING_CLR(p_uart)             ((p_uart)->STA = LL_UARTX_STA_TC_PENDING)

/**
  * @brief  Link layer UART Rx timeout pending get
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval result
  */
#define LL_UART_RX_TIMEOUT_PENDING_GET(p_uart)              ((p_uart)->STA & LL_UARTX_STA_RX_TIMEOUT_PENDING)
/**
  * @brief  Link layer UART Rx timeout pending clr
  * @param  p_tk: The structure pointer of the UART is selected
  * @retval None
  */
#define LL_UART_RX_TIMEOUT_PENDING_CLR(p_uart)              ((p_uart)->STA = LL_UARTX_STA_RX_TIMEOUT_PENDING)

/**
  * @brief  Link layer UART Rx ferr pending get
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval result
  */
#define LL_UART_RX_FERR_PENDING_GET(p_uart)                 ((p_uart)->STA & LL_UARTX_STA_RX_FERR_PENDING)
/**
  * @brief  Link layer UART Rx ferr pending clr
  * @param  p_tk: The structure pointer of the UART is selected
  * @retval None
  */
#define LL_UART_RX_FERR_PENDING_CLR(p_uart)                 ((p_uart)->STA = LL_UARTX_STA_RX_FERR_PENDING)

/**
  * @brief  Link layer UART Rx buf overflow pending get
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval result
  */
#define LL_UART_RX_BUF_OV_PENDING_GET(p_uart)               ((p_uart)->STA & LL_UARTX_STA_RX_BUF_OV_PENDING)
/**
  * @brief  Link layer UART Rx buf overflow pending clr
  * @param  p_tk: The structure pointer of the UART is selected
  * @retval None
  */
#define LL_UART_RX_BUF_OV_PENDING_CLR(p_uart)               ((p_uart)->STA = LL_UARTX_STA_RX_BUF_OV_PENDING)

/**
  * @brief  Link layer UART Rx buf not empty pending get
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval result
  */
#define LL_UART_RX_BUF_NOT_EMPTY_PENDING_GET(p_uart)        ((p_uart)->STA & LL_UARTX_STA_RX_BUF_NOT_EMPTY_PENDING)
/**
  * @brief  Link layer UART Rx buf not empty pending clr
  * @param  p_tk: The structure pointer of the UART is selected
  * @retval None
  */
#define LL_UART_RX_BUF_NOT_EMPTY_PENDING_CLR(p_uart)        ((p_uart)->STA = LL_UARTX_STA_RX_BUF_NOT_EMPTY_PENDING)

/**
  * @brief  Link layer UART Tx finish pending get
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval result
  */
#define LL_UART_TX_BUF_EMPTY_PENDING_GET(p_uart)            ((p_uart)->STA & LL_UARTX_STA_TX_BUF_EMPTY_PENDING)
/**
  * @brief  Link layer UART Tx finish pending clr
  * @param  p_uart: The structure pointer of the UART is selected
  * @retval result
  */
#define LL_UART_TX_BUF_EMPTY_PENDING_CLR(p_uart)            ((p_uart)->STA = LL_UARTX_STA_TX_BUF_EMPTY_PENDING)

/**
  * @brief  uart tx
  * @param  p_uart: The structure pointer of the UART group (UART0, UART1) is selected.
  * @param  tx_data: tx uart data.
  * @retval result.
  * @note   Uart tx done pending need clear in this function, because, not using irq.
  */
void ll_uart_tx(UART_TypeDef *p_uart,u16 tx_data);

/**
  * @brief  uart rx
  * @param  p_uart: The structure pointer of the UART group (UART0, UART1) is selected.
  * @param  rx_data: uart rx data.
  * @retval true or false.
  * @note   uart rx buf not empty  need clear or not in this function, because, chip can do when rx buf is empty.
  */
bool ll_uart_rx(UART_TypeDef *p_uart, u16 *rx_data);

/**
  * @brief  uart irq tx
  * @param  p_uart: The structure pointer of the UART group (UART0) is selected.
  * @param  tx_data: tx uart data.
  * @retval result.
  * @note   uart tx done pending need clear in irq interrupt,and tx next data in the uart irq interrupt.
  */
void ll_uart_irq_tx(UART_TypeDef *p_uart, u16 tx_data);

/**
  * @brief  uart baudrate set
  * @param  p_uart: The structure pointer of the UART group (UART0) is selected.
  * @param  baudrate: uart baudrate.
  * @retval result.
  */
void ll_uart_baudrate_set(UART_TypeDef *p_uart, uint32_t baudrate);

/**
  * @brief  uart work mode get(duplex, single send, single receive, single auto)
  * @param  p_uart: The structure pointer of the UART group (UART0) is selected.
  * @retval work mode enum.
  */
TYPE_ENUM_LL_UART_WORK_MODE ll_uart_work_mode_get(UART_TypeDef *p_uart);

/**
  * @brief  uart work mode set(duplex, single send, single receive, single auto)
  * @param  p_uart: The structure pointer of the UART group (UART0) is selected.
  * @param  work_mode: work mode enum.
  * @retval result.
  */
void ll_uart_work_mode_set(UART_TypeDef *p_uart, TYPE_ENUM_LL_UART_WORK_MODE work_mode);

/**
  * @brief  uart tx inv en get
  * @param  p_uart: The structure pointer of the UART group (UART0) is selected.
  * @retval ENABLE or DISABLE.
  */
FunctionalState ll_uart_tx_inv_en_get(UART_TypeDef *p_uart);

/**
  * @brief  uart rx inv en get
  * @param  p_uart: The structure pointer of the UART group (UART0) is selected.
  * @retval ENABLE or DISABLE.
  */
FunctionalState ll_uart_rx_inv_en_get(UART_TypeDef *p_uart);

/**
  * @brief  uart 9bit en get
  * @param  p_uart: The structure pointer of the UART group (UART0) is selected.
  * @retval ENABLE or DISABLE.
  */
FunctionalState ll_uart_9bit_en_get(UART_TypeDef *p_uart);


/** 
  * @brief  uart config
  * @param  p_uart: The structure pointer of the UART group (UART1) is selected.
  * @param  baudrate: time interval.
  * @param  parity: parity
  * @retval result.
  * @note   unit : uart clock, bit[16]~bit[31], time interval from re falling edge to de rising edge
  */
void ll_uart_config(UART_TypeDef *p_uart, u32 baudrate, TYPE_ENUM_LL_UART_PARITY_SEL parity);

/** 
  * @brief  uart config extend
  * @param  p_uart: The structure pointer of the UART group (UART1) is selected.
  * @param  baudrate: time interval.
  * @param  bit_width_sel: 8 or 9
  * @param  parity: None Odd Even
  * @param  stop_bit_sel: 1 or 2
  * @param  work_mode : work method
  * @retval result.
  * @note   unit : uart clock, bit[16]~bit[31], time interval from re falling edge to de rising edge
  */
void ll_uart_config_ex(UART_TypeDef *p_uart, u32 baudrate, TYPE_ENUM_LL_UART_BIT_WIDTH_SEL bit_width_sel, \
                       TYPE_ENUM_LL_UART_PARITY_SEL parity, TYPE_ENUM_LL_UART_STOP_BIT_SEL stop_bit_sel, TYPE_ENUM_LL_UART_WORK_MODE  work_mode);

/** 
  * @brief  uart work mode config
  * @param  p_uart: The structure pointer of the UART group (UART1) is selected.
  * @param  work_mode : work method
  */
void ll_uart_mode_sel(UART_TypeDef *p_uart, TYPE_ENUM_LL_UART_WORK_MODE work_mode);



/** 
  * @brief  uart set timeout bit len
  * @param  p_uart: The structure pointer of the UART group (UART1) is selected.
  * @param  timeout_bit : timeout bit
  * @note   timeout time = (timeout_bit + 1)*Bit_Rate     defalut timeout time = (55+1)*Bit_Rate
  */
void ll_uart_set_timeout_bit_len(UART_TypeDef *p_uart, u8 timeout_bit);

/**
  * @}
  */

/**
  * @}
  */

#ifdef __cplusplus
}
#endif

/**
  * @}
  */

/**
  * @}
  */

#endif //__JS32T031_LL_UART_H

/*************************** (C) COPYRIGHT 2022 JUSHENG ***** END OF FILE *****/
